<template>
  <div class="box">
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">  
          <el-input clearable placeholder="请输入内容" v-model="queryInfo.query" @clear="getGoodsList" >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">  
          <el-button type="primary" @click="btnAddGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 用户列表区域 -->
      <el-table :data="goodsList" border stripe style="width: 100%">
        <el-table-column type="index" label="序号" width="50px" >
          <template slot-scope="scope">{{scope.row.index}}</template>
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110px" ></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80px" ></el-table-column>
        <el-table-column label="创建时间" width="160px" >
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!-- 修改 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="btnEditGoods(scope.row.goods_id)"></el-button>
            <!-- 删除 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="btnDeleteGoods(scope.row.goods_id)"></el-button>    
          </template>        
        </el-table-column>
      </el-table>    
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5,10,15,20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>  
    </el-card>  
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        // 搜索请求参数
        query: "",
        // 当前页数
        pagenum: 1,
        // 每页显示多少数据
        pagesize: 10,
      },
      goodsList: [],
      total: 0,
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    // 获取商品列表 如果queryInfo.query为空，则全部查询，queryInfo.query不为空，则参数查询
    async getGoodsList() {
      // get 方法返回一个 promise 对象
      const { data: res } = await this.$axios.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status != 200) {
        return this.$message.error(res.meta.msg);
      }
      this.$message.success(res.meta.msg);
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      // 为一级数据添加索引
      for (let i = 0; i < this.goodsList.length; i++) {
        this.goodsList[i].index =
          i + 1 + (this.queryInfo.pagenum - 1) * this.queryInfo.pagesize;
      }
    },
    // 改变每页显示多少行
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    // 改变当前页码
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    // 添加商品按钮
    btnAddGoods() {
      this.$router.push("goods/add");
    },
    // 修改商品按钮
    async btnEditGoods(id) {},
    // 删除商品按钮
    btnDeleteGoods(goods_id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 发起删除的网络请求
          const { data: res } = await this.$axios.delete(`goods/${goods_id}`);
          // 删除失败
          if (res.meta.status != 200) {
            return this.$message.error(res.meta.msg);
          }
          // 删除成功
          this.$message.success(res.meta.msg);
          // 刷新数据
          this.getGoodsList();
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
  },
};
</script>

<style>
</style>